package com.bsj.sipgateway.util;


import com.bsj.sipgateway.config.SipConfig;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Description:SIP信令中的SSRC工具类。SSRC值由10位十进制整数组成的字符串，第一位为0代表实况，为1则代表回放；第二位至第六位由监控域ID的第4位到第8位组成；最后4位为不重复的4个整数
 * @author bsj-chenjunkang
 * @date 2022/8/23
 */
@Slf4j
public class SsrcUtil {

    /**
     * 获取视频预览的SSRC值,第一位固定为0
     *
     */

    public static String getPlaySsrc(long id,String channelId ){
        return "0" +String.format("%06d",id)+String.format("%03d",Integer.parseInt(channelId));
    }

    /**
     * 获取录像回放的SSRC值,第一位固定为1
     *
     */
    public static String getPlayBackSsrc(long vehicleId,String channelSerial) {
        return "1"+String.format("%06d",vehicleId)+String.format("%03d",Integer.parseInt(channelSerial));
    }

    public static SsrcInfo analysisSsrcInfo(long ssrc) {
        // 完整10位ssrc信息
        String completeSsrc = formatInt(ssrc);
        return new SsrcInfo(Integer.parseInt(completeSsrc.substring(0, 1)) == 1 ? true : false, Integer.parseInt(completeSsrc.substring(1, 7)), Integer.parseInt(completeSsrc.substring(7)));
    }
    /**
     * 返回长度为10的ssrc
     * @param num:
     * @return
     **/
    public static String formatInt(long num) {
        String str = Long.toString(num);
        if (str.length() > 10) {
            str = str.substring(str.length() - 10);
        } else if (str.length() < 10) {
            StringBuilder sb = new StringBuilder(str);
            while (sb.length() < 10) {
                sb.insert(0, '0');
            }
            str = sb.toString();
        }
        return str;
    }

    public static class SsrcInfo {
        /**
         * 是否实时视频
         */
        public boolean isReadVideo;
        /**
         * 车辆id
         */
        public int vhieleId;
        /**
         * 通道号
         */
        public int channel;

        public SsrcInfo(boolean isReadVideo, int vhieleId, int channel) {
            this.isReadVideo = isReadVideo;
            this.vhieleId = vhieleId;
            this.channel = channel;
        }

        @Override
        public String toString() {
            return "SsrcInfo{" +
                    "isReadVideo=" + isReadVideo +
                    ", vhieleId=" + vhieleId +
                    ", channel=" + channel +
                    '}';
        }
    }
}
